from django.conf import settings
from django.utils.translation import ugettext as _
from google.appengine.api import memcache
from google.appengine.api import users
from looptube.models import UserLoopedVideo
from looptube.utils import fetchEntriesFromAtomFeed

channelDict = {'britfashion' : 'QRadio', 
               'nobel' : 'XM-Ethel', 
               'altcafe' : 'XM-Cafe',
               'tophits' : 'XM-20-20',
               'liquid' : 'XM-Watercolors',
               'globalpop' : 'XM-UPop',
               'hits2k' : 'XM-Pop2K',
               }
channelNameDict = {'britfashion' : 'British Fashion', 
                   'nobel' : 'Nobel', 
                   'altcafe' : 'Alternative Cafe',
                   'tophits' : 'Top Hits',
                   'liquid' : 'Liquid',
                   'globalpop' : 'Global Pop',
                   'hits2k' : 'Hits 2K',
                   'highlight' : _('My highlight videos')
                   }
channelDesc = {'britfashion' : _('British Rock Style'),
               'nobel' : _('Modern Rock'),
               'altcafe' : _('Alternative Tracks'),
               'tophits' : _('Latest Top Hits'),
               'liquid' : _('Smooth Jazz Music'),
               'globalpop' : _('International Hits'),
               'hits2k' : _('Modern Hits'),
               'highlight' : _('My highlight videos')
               }

#functions
def getChannelDict():
    return channelDict

def getChannelNameDict():
    return channelNameDict

def commonInit(request):
    contextDict = {}
    
    siteHostname = request.get_host();
    contextDict['siteHostname'] = siteHostname
    contextDict['siteName'] = settings.SITE_NAME
    contextDict['requestURL'] = request.build_absolute_uri()
    
    # debug
    if (siteHostname.startswith('localhost')):
        contextDict['firebugEnabled'] = settings.FIREBUG_ENABLED

    user = users.get_current_user()
    if (user):
        contextDict['user'] = user
        userRecentLoopedVideos = UserLoopedVideo.getUserRecentLoopedVideos(user, 10)
        if (len(userRecentLoopedVideos) > 0):
            contextDict['userRecentLoopedVideos'] = userRecentLoopedVideos
        
    contextDict['loginURL'] = users.create_login_url(request.build_absolute_uri())
    contextDict['logoutURL'] = users.create_logout_url(request.build_absolute_uri())

    if (request.COOKIES.__contains__('noshare')):
        contextDict['noshare'] = request.COOKIES['noshare']

    contextDict['channelDesc'] = channelDesc
    contextDict['channelDict'] = channelDict
    contextDict['channelNameDict'] = channelNameDict
    
    # fetch news from dev blog
    devBlogItems = memcache.get('devBlogItems')
    if (None == devBlogItems):
        devBlogItems = fetchEntriesFromAtomFeed('http://looptube.blogspot.com/feeds/posts/default')
        if (devBlogItems):
            devBlogItems = devBlogItems[:3]
            memcache.add('devBlogItems', devBlogItems, 3600)
    for entry in devBlogItems:
        entry['published'] = entry['published'][5:]
    if (len(devBlogItems) > 0):
        devBlogItems[0]['red'] = True
    contextDict['devBlogNews'] = devBlogItems

    siteMenuItems = { 'channels' : { 'name' : _('Channels'), 'url' : '/channel', 
                                     'subMenuItems' : [ {'name' : channelNameDict[name], 'url' : '/channel/play/'+name} for name in channelNameDict ] },
                      'playlist' : { 'name' : _('Playlists'), 'url' : '',
                                     'subMenuItems' : [ {'name' : _('Create new playlist'), 'url' : '/playlist/create'} ] }
                    }
    if (user):
        siteMenuItems['playlist']['subMenuItems'].append({'name' : _('My Playlists'), 'url' : '/playlist/user/'+user.email()})
    contextDict['siteMenuItems'] = [siteMenuItems[name] for name in siteMenuItems]
    contextDict['breadcrumbs'] = [ { 'name' : _('Home Page'), 'url' : '/' } ]
    return contextDict
